﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;

namespace DiaosFix.Core
{
    public class UserAreaOperator
    {
        private UsersOperator uo = new UsersOperator();
        private DiaosFixEntities context = new DiaosFixEntities();

        public Response<List<RequestArea>> GetAreaByUser(Users u)
        {
            var response = new Response<List<RequestArea>>();

            if (u.UserType.Name == "Diaos")
            {
                response.IsSuccess = true;
                response.Message = string.Empty;
                response.Item = u.RequestArea.ToList();
            }
            else
            {
                response.IsSuccess = false;
                response.Message = "This user is not a diaos, invalid request for GetAreaByUser";
                response.Item = null;
            }
            return response;
        }

        public Response<List<RequestArea>> GetAreaByUser(string userName)
        {
            var userResponse = uo.FindUser(userName);
            if (userResponse.IsSuccess)
            {
                return GetAreaByUser(userResponse.Item);
            }
            else
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = false,
                    Message = "Cannot find the user",
                    Item = null
                };
            }
        }

        public Response<List<Users>> GetUsersByArea(int areaId)
        {
            var query = context.RequestArea.Where(p => p.Id == areaId).SingleOrDefault();
            if (null != query)
            {
                return new Response<List<Users>>()
                {
                    IsSuccess = true,
                    Message = string.Empty,
                    Item = query.Users.ToList()
                };
            }
            else
            {
                return new Response<List<Users>>()
                {
                    IsSuccess = false,
                    Message = "Cannot find the diaos who in charge of this area",
                    Item = null
                };
            }
        }

        public Response<List<RequestArea>> AddAreaToUser(Users u, List<string> areaNames)
        {
            if (null == u || null == areaNames || areaNames.Count == 0)
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = false,
                    Message = "Parameter null or empty",
                    Item = null
                };
            }

            if (u.UserType.Name != "Diaos")
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = false,
                    Message = "this user is not a diaos",
                    Item = null
                };
            }

            var list = from item in areaNames
                       select new RequestArea()
                       {
                           AreaName = item
                       };

            return AddAreaToUser(u, list.ToList());
        }

        public Response<List<RequestArea>> AddAreaToUser(Users u, List<RequestArea> areas)
        {
            if (null == u || null == areas || areas.Count == 0)
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = false,
                    Message = "Parameter null or empty",
                    Item = null
                };
            }

            if (u.UserType.Name != "Diaos")
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = false,
                    Message = "this user is not a diaos",
                    Item = null
                };
            }

            var response = new Response<List<RequestArea>>();

            foreach (var item in areas)
            {
                u.RequestArea.Add(item);
            }

            if (context.SaveChanges() > 0)
            {
                response.IsSuccess = true;
                response.Message = string.Empty;
                response.Item = u.RequestArea.ToList();
            }
            else
            {
                response.IsSuccess = false;
                response.Message = "Error saving into Database";
                response.Item = null;
            }

            return response;
        }

        public Response<List<RequestArea>> UpdateUserArea(Users u, List<RequestArea> areas)
        {
            if (u.UserType.Name != "Diaos")
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = false,
                    Message = "this user is not a diaos",
                    Item = null
                };
            }

            var entityCollection = new EntityCollection<RequestArea>();
            foreach (var item in areas)
            {
                entityCollection.Add(item);
            }

            u.RequestArea = entityCollection;

            if (context.SaveChanges() > 0)
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = true,
                    Message = string.Empty,
                    Item = u.RequestArea.ToList()
                };
            }
            else
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = false,
                    Message = "error saving to Database",
                    Item = null
                };
            }
        }

        public Response<List<RequestArea>> DeleteAreaOfUser(Users u, List<RequestArea> areas)
        {
            if (u.UserType.Name != "Diaos")
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = false,
                    Message = "this user is not a diaos",
                    Item = null
                };
            }

            foreach (var item in areas)
            {
                u.RequestArea.Remove(item);
            }

            if (context.SaveChanges() > 0)
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = true,
                    Message = string.Empty,
                    Item = u.RequestArea.ToList()
                };
            }
            else
            {
                return new Response<List<RequestArea>>()
                {
                    IsSuccess = false,
                    Message = "error saving to Database",
                    Item = null
                };
            }
        }
    }
}
